<?php
declare (strict_types=1);

namespace app\controller;

use app\handler\Aes;
use app\handler\JwtAuth;
use app\helpers\api\ApiResponse;
use app\model\User;
use think\Request;

class TokenController
{
    use ApiResponse;

    //登录
    public function store(Aes $aes, Request $request)
    {
        $user = User::field('name')->where([
            'name'     => $request->param('name'),
            'password' => $aes->encode($request->param('password'))//加密密码
        ])->find();
        if ($user) {
            return $this->success($user);
        }
        return $this->failed('用户名或身份证错误');
    }

    /**
     * 注册
     * @param Request $request
     * @return mixed
     */
    public function registere(Aes $aes, Request $request)
    {
        //验证参数，用户名必须唯一
        $validate = new \app\validate\User();
        $res      = $validate->check($request->param());
        if (!$res) {
            return $this->error($validate->getError());
        }
        $data             = $request->param();
        $data['password'] = $aes->encode($data['password']);
        User::create($data);
        return $this->created();
    }

}
